package qq.com;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class QQClient {

	public static void register(String user, String pwd) {
		try {
			//1.寤虹珛鍒版湇鍔″櫒9999绔彛鐨勮繛鎺�
			Socket socket = new Socket("127.0.0.1",9999);
			//2.鑾峰彇JVM鍒皊ocket鐨勮緭鍑烘祦
			OutputStream os = socket.getOutputStream();
			//3.灏嗗師鐢熺殑瀛楄妭杈撳嚭娴佸皝瑁呬负瀵硅薄杈撳嚭娴�
			ObjectOutputStream oos = new ObjectOutputStream(os);
			//4.灏佽璇锋眰鍙傛暟QQRequest
			QQRequest request = new QQRequest();
			request.setOperator(1);//1浠ｈ〃娉ㄥ唽鎿嶄綔
			request.setUsername(user);
			request.setPassword(pwd);
			//5.灏嗚姹傚璞equest鍐欏叆socket
			oos.writeObject(request);
			oos.flush();
			//6.鎺ユ敹娉ㄥ唽缁撴灉锛堟湇鍔″櫒杩斿洖鐨勶級
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//灏佽鍘熺敓杈撳叆娴佷负瀵硅薄娴侊紝鏂逛究瀵硅薄璇诲彇
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			System.out.println(resp);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static QQResponse login(String user, String password) {

		try {
			//1.杩炴帴鏈嶅姟鍣�
			Socket socket = new Socket("127.0.0.1",9999);
			//2.鑾峰彇杈撳嚭娴佸苟灏佽涓哄璞℃祦
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest(2,user,password);//2浠ｈ〃鐧诲綍鎿嶄綔
			oos.writeObject(request);
			oos.flush();
			//3.鎺ユ敹娉ㄥ唽缁撴灉锛堟湇鍔″櫒杩斿洖鐨勶級
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//灏佽鍘熺敓杈撳叆娴佷负瀵硅薄娴侊紝鏂逛究瀵硅薄璇诲彇
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			System.out.println(resp);
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static QQResponse addFriend(String user, String friend) {
		//1.杩炴帴鏈嶅姟鍣�
		try {
			//1.杩炴帴鏈嶅姟鍣�
			Socket socket = new Socket("127.0.0.1",9999);
			//2.鑾峰彇杈撳嚭娴佸苟灏佽涓哄璞℃祦
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();//2浠ｈ〃鐧诲綍鎿嶄綔
			request.setOperator(3);//娣诲姞濂藉弸
			request.setUsername(user);
			request.setFriend(friend);
			oos.writeObject(request);
			oos.flush();
			//6.鎺ユ敹娉ㄥ唽缁撴灉锛堟湇鍔″櫒杩斿洖鐨勶級
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//灏佽鍘熺敓杈撳叆娴佷负瀵硅薄娴侊紝鏂逛究瀵硅薄璇诲彇
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static QQResponse findFriendsInfo(String user) {
		//1.杩炴帴鏈嶅姟鍣�
		try {
			//1.杩炴帴鏈嶅姟鍣�
			Socket socket = new Socket("127.0.0.1",9999);
			//2.鑾峰彇杈撳嚭娴佸苟灏佽涓哄璞℃祦
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();
			request.setOperator(4);//鏌ヨ濂藉弸鍒楄〃
			request.setUsername(user);
			oos.writeObject(request);
			oos.flush();
			//6.鎺ユ敹娉ㄥ唽缁撴灉锛堟湇鍔″櫒杩斿洖鐨勶級
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//灏佽鍘熺敓杈撳叆娴佷负瀵硅薄娴侊紝鏂逛究瀵硅薄璇诲彇
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

    public static QQResponse sendMsg(String user, String friend, String show) {
		try {
			//1.杩炴帴鏈嶅姟鍣�
			Socket socket = new Socket("127.0.0.1",9999);
			//2.鑾峰彇杈撳嚭娴佸苟灏佽涓哄璞℃祦
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			QQRequest request = new QQRequest();
			request.setOperator(5);//鍙戦�佽亰澶╂秷鎭�
			request.setUsername(user);
			request.setFriend(friend);
			request.setMsg(show);
			oos.writeObject(request);
			oos.flush();
			//6.鎺ユ敹娉ㄥ唽缁撴灉锛堟湇鍔″櫒杩斿洖鐨勶級
			InputStream is = socket.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);//灏佽鍘熺敓杈撳叆娴佷负瀵硅薄娴侊紝鏂逛究瀵硅薄璇诲彇
			Object readObject = ois.readObject();
			QQResponse resp = (QQResponse)readObject;
			return resp;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
    }
}
